<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body custom-class="import-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="有效期" prop="validTime">
          <el-select v-model="form.validTime" placeholder="请选择" @change="validTimeChange">
            <el-option :label="item.label" :value="item.value" v-for="(item,index) in validTimeList" :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="loading">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { debounce } from '@/utils'
import { addLockPwd } from '@/api/smartLock/lockManage'
import { getDate } from '@/utils/common/moment'

export default {
  components: {},
  props: {
    title: {
      default: '新增时效密码'
    },
    value: {
      default: false
    },
    data: {
      default: undefined
    }
  },
  data() {
    return {
      //是否显示选择房间弹窗
      dialogSelectRoomVisible: false,
      form: {},
      validTimeList: [
        { label: '2小时', value: 2,type:'hours' },
        { label: '6小时', value: 6, type:'hours' },
        { label: '12小时', value: 12,type: 'hours' },
        { label: '1天', value: 24,type:'hours' },
        { label: '2天', value: 48,type:'hours' },
        { label: '长期', value: 10,type:'years' }
      ],
      rules: {
        validTime: [
          { required: true, message: '请选择有效期', trigger: 'change' }
        ]
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: []
    }
  },
  computed: {
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.init()
        }
      },
      immediate: true
    }
  },
  methods: {
    init() {
      this.reset()
    },
    reset() {
      this.form = {
        lockId: this.data.lockId,
        authStartTime: '',
        authEndTime: '',
        validTime: 2,
        timeType: 'hours'
      }
      this.resetForm('form')
      this.validTimeChange(this.form.validTime)
    },
    // 选择有效期
    validTimeChange(val) {
      let obj  = this.validTimeList.find(item => item.value === val)
      this.form.authStartTime = this.$moment().format('YYYY-MM-DD HH:mm:ss')
      this.form.authEndTime = getDate(this.$moment().format('YYYY-MM-DD HH:mm:ss'), obj.value, 'YYYY-MM-DD HH:mm:ss', obj.type)
      console.log('this.form.authEndTime',this.form)
      this.form.validTime = obj.value
      this.form.timeType = obj.type
    },
    // 确认
    submitForm: debounce(function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let params = {
            lockId: this.form.lockId,
            authStartTime: this.form.authStartTime,
            authEndTime: this.form.authEndTime
          }
          // params.authStartTime = this.$moment().format('YYYY-MM-DD HH:mm:ss')
          // params.authEndTime = getDate(params.authStartTime, this.form.validTime, 'YYYY-MM-DD HH:mm:ss', 'hours')
          this.loading = true
          addLockPwd(params).then(response => {
            this.loading = false
            if (response.code === 200) {
              this.$emit('success')
              this.$message.success(response.msg)
              this.visible = false
            } else {
              this.$message.error(response.msg)
            }
          }).catch(error => {
            this.loading = false
            console.log(error)
          })
        }
      })
    }, 500, false),
    cancel() {
      this.reset()
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/dialog";
</style>
